package com.wql.service.impl;

import com.aaa.entity.BAppeal;
import com.aaa.entity.BBusinessInfo;
import com.aaa.entity.GGoods;
import com.aaa.entity.GSort;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wql.feign.OssFeign;
import com.wql.feign.UserInfoFeign;
import com.wql.mapper.BAppealMapper;
import com.wql.service.BBusinessInfoService;
import com.wql.mapper.BBusinessInfoMapper;
import com.wql.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author 咕噜咕噜
* @description 针对表【b_business_info(商家信息表)】的数据库操作Service实现
* @createDate 2023-03-03 16:19:51
*/
@Service
public class BBusinessInfoServiceImpl extends ServiceImpl<BBusinessInfoMapper, BBusinessInfo>
    implements BBusinessInfoService{

    @Autowired
    private  BBusinessInfoMapper bBusinessInfoMapper;

    @Autowired
    private UserInfoFeign userInfoFeign;

    @Autowired
    private OssFeign ossFeign;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private BAppealMapper bAppealMapper;

    @Override
    public Result getBusinessInfo() {
        HttpServletRequest request = WebUtils.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username = tokenChaim.get("username");

        String s = username.toString();
        QueryWrapper<BBusinessInfo> wrapper = new QueryWrapper<>();

        wrapper.eq("telephone",username);
        BBusinessInfo bBusinessInfo = bBusinessInfoMapper.selectOne(wrapper);
        if (bBusinessInfo!=null){
            return  new Result(2000,"查询信息成功",bBusinessInfo);
        }else {
            return new Result(5000,"信息显示失败，请联系管理员");
        }
    }

    @Override
    public Result changeshop(BBusinessInfo businessInfo) {
         bBusinessInfoMapper.updateById(businessInfo);
        return new Result(2000,"保存成功");
    }

    @Override
    public Result changePassword(String telephone, String password,String code) {
        Result verify = userInfoFeign.verify(code);
        if (verify.getCode()==2000){
            String password1 = passwordEncoder.encode(password);
            bBusinessInfoMapper.changePassword(telephone,password1);
            return new Result(2000,"修改成功");
        }else {
            return new Result(5000,"修改失败,验证码错误");
        }
    }

    @Override
    public Result getMessage(String telephone) {
        userInfoFeign.sms(telephone);
        return new Result(2000,"验证码发送成功");
    }

    @Override
    public Result upload(MultipartFile file) {
        Result updateload = ossFeign.updateload(file);
        return updateload;
    }


    @Override
    public Result insert(BBusinessInfo bBusinessInfo) {

        int insert = bBusinessInfoMapper.insert(bBusinessInfo);
        return new Result(2000,"添加成功",insert);
    }

    @Override
    public Result sortname() {
        List<GSort> sortname = bBusinessInfoMapper.sortname();
        return new Result(2000,"查询成功",sortname);
    }

    @Override
    public Result shangjiashensu(BAppeal bAppeal) {
        HttpServletRequest request = WebUtils.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username = tokenChaim.get("username");

        String telephone = username.toString();
        QueryWrapper<BBusinessInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone",telephone);
        BBusinessInfo bBusinessInfo = bBusinessInfoMapper.selectOne(wrapper);

        Date date = new Date();
        String date1 = date.toString();
        bAppeal.setBusinessId(bBusinessInfo.getId());
        bAppeal.setAppealTime(date1);
        bAppeal.setStatus(0);
        bAppealMapper.insert(bAppeal);

        return new Result(2000,"申诉成功，请等待管理员审核");
    }


}




